<include file="Public/head" />
<script type="text/javascript" src="__PUBLIC__/js/jquery-latest.js"></script>
<script type="text/javascript" src="__PUBLIC__/ueditor/1.4.3/ueditor.config.js"></script>
<script type="text/javascript" src="__PUBLIC__/ueditor/1.4.3/ueditor.all.min.js"> </script>

<link rel="stylesheet" href="__PUBLIC__/zyUpload/css/zyUpload.css" type="text/css">
<script type="text/javascript" src="__PUBLIC__/zyUpload/js/zyFile.js"></script>
<script type="text/javascript" src="__PUBLIC__/zyUpload/js/zyUpload.js"></script>

<body>
  <script>
    $(function(){
      $("#zyUpload").zyUpload({
        width:"650px",height:"",itemWidth:"120px",itemHeight:"100px",
        url:"{:U('Goods/zyUpdate')}",multiple:true,
        dragDrop:true,del:true,finishDel:false
      });
    });
    var ue = UE.getEditor('editor');

    /*******************/
    var step = {
      Creat_Table: function() {
        step.hebingFunction();
        var SKUObj = $('.Father_Title');
        var arrayTile = new Array();
        var arrayInfor = new Array();
        var arrayColumn = new Array();
        var bCheck = true;
        var columnIndex = 0;

        $.each(SKUObj, function(i, item) {
          arrayColumn.push(columnIndex++);
          arrayTile.push(SKUObj[i].value);
          var itemName = $('.check_item')[i].value.split("/");
          var bCheck2 = true;

          var order = new Array();
          $.each(itemName,function(y, item) {
            order.push(itemName[y]);
          });

          arrayInfor.push(order);
          if (order.join() == '') {
            bCheck = false;
          }
        })
        if (bCheck) {
          $('#createTable').html('');
          var table = $('<table id="process" class="columnList"></table>');
          table.appendTo($('#createTable'));
          var thead = $('<thead></thead>');
          thead.appendTo(table);
          var trHead = $('<tr></tr>');
          trHead.appendTo(thead);
          var str = '';
          $.each(arrayTile, function(index, item) {
            str += '<th width="100">' + item + '</th>';
          })
          str += '<th width="200">销售价</th><th width="200">市场价</th><th width="200">库存</th><th width="50">默认</th>';
          trHead.append(str);
          var tbody = $('<tbody></tbody>');
          tbody.appendTo(table);

          var zuheDate = step.doExchange(arrayInfor);
          if (zuheDate.length > 0) {
            $.each(zuheDate, function(index, item) {
              var td_array = item.split(',');
              var tr = $('<tr></tr>');
              tr.appendTo(tbody);
              var str = '';
              var temp = '';
              $.each(td_array, function(i, values) {
                str += '<td>' + values + '</td>';
                temp += values + '/';
              });
              str += '<td><input name="it_reprice[]" class="inpbox inpbox-mini" value="0" type="text"></td>';
              str += '<td><input name="it_maprice[]" class="inpbox inpbox-mini" value="0" type="text"></td>';
              str += '<td><input name="it_stock[]" class="inpbox inpbox-mini" value="0" type="text"></td>';
              str += '<td><input name="checked" value="'+index+'" type="radio">';
              str += '<input name="it_name[]" value="'+temp+'" type="hidden"></td>';
              tr.append(str);
            });
          }
          arrayColumn.pop();
          $(table).mergeCell({
            cols: arrayColumn
          });
        } else {
          document.getElementById('createTable').innerHTML = "";
        }
      },
      hebingFunction: function() {
        $.fn.mergeCell = function(options) {
          return this.each(function() {
            var cols = options.cols;
            for (var i = cols.length - 1; cols[i] != undefined; i--) {
              mergeCell($(this), cols[i]);
            }
            dispose($(this));
          })
        };

        function mergeCell($table, colIndex) {
          $table.data('col-content', '');
          $table.data('col-rowspan', 1);
          $table.data('col-td', $());
          $table.data('trNum', $('tbody tr', $table).length);
          $('tbody tr', $table).each(function(index) {
            var $td = $('td:eq(' + colIndex + ')', this);
            var currentContent = $td.html();
            if ($table.data('col-content') == '') {
              $table.data('col-content', currentContent);
              $table.data('col-td', $td);
            } else {
              if ($table.data('col-content') == currentContent) {
                var rowspan = $table.data('col-rowspan') + 1;
                $table.data('col-rowspan', rowspan);
                $td.hide();
                if (++index == $table.data('trNum'))
                  $table.data('col-td').attr('rowspan', $table.data('col-rowspan'));
              }
              else {
                if ($table.data('col-rowspan') != 1) {
                  $table.data('col-td').attr('rowspan', $table.data('col-rowspan'));
                }
                $table.data('col-td', $td);
                $table.data('col-content', $td.html());
                $table.data('col-rowspan', 1);
              }
            }
          })
        }
        function dispose($table) {
          $table.removeData();
        }
      },
      doExchange: function(doubleArrays) {
        var len = doubleArrays.length;
        if (len >= 2) {
          var arr1 = doubleArrays[0];
          var arr2 = doubleArrays[1];
          var len1 = arr1.length;
          var len2 = arr2.length;
          var newLen = len1 * len2;
          var temp = new Array(newLen);
          var index = 0;
          for (var i = 0; i < len1; i++) {
            for (var j = 0; j < len2; j++) {
              temp[index++] = arr1[i] + ',' + arr2[j];
            }
          }
          var newArray = new Array(len - 1);
          newArray[0] = temp;
          if (len > 2) {
            var _count = 1;
            for (var i = 2; i < len; i++) {
              newArray[_count++] = doubleArrays[i];
            }
          }
          return step.doExchange(newArray);
        } else {
          return doubleArrays[0];
        }
      }
    }
    /*******************/
  </script>

  <div class="panel admin-panel">
    <div class="body-content">
      <form method="post" class="form-x" action="" enctype="multipart/form-data">

        <div class="form-group">
          <div class="label">
            <label>{:L("shangpinmingcheng")}：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" value="{$info['goods_name']}" name="goods_name" data-validate="required:{:L('qingshuru')}{:L('shangpinmingcheng')}" />
            <div class="tips"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label>{:L("shangpinfenlei")}：</label>
          </div>
          <div class="field">
            <select name="goods_cate_id" class="input w50">
              <empty name="info['goods_cate_id']">
                <option value="0">{:L("qingxuanze")}</option>
              </empty>
              <foreach name="cate_list" item="v">
                <option value="{$v['goods_cate_id']}" <if condition="$v['goods_cate_id'] eq $info['goods_cate_id']">selected="selected"</if>>{$v['goods_cate_name']}</option>
                <if condition="$v['sonlist']">
                  <foreach name="v['sonlist']" item="v2">
                    <option value="{$v2['goods_cate_id']}" <if condition="$v2['goods_cate_id'] eq $info['goods_cate_id']">selected="selected"</if>>---{$v2['goods_cate_name']}</option>
                  </foreach>
                </if>
              </foreach>
            </select>
            <div class="tips"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label>商品图：</label>
          </div>
          <div class="field">
            <input type="file" class="button bg-blue" name="default_img" value="+ {:L('linlanshangchuan')}" onchange="preview(this)" style="float: left;">
            <div id="preview" name="img" style="float: left;width: 10%;box-sizing: border-box;" class="input">
              <empty name="info['default_img']">
                <img src="__PUBLIC__/images/zanwu.jpg" width="100%" alt=""/>
                <else />
                <img src="__ROOT__/{$info['default_img']}" width="100%" alt=""/>
              </empty>
            </div>
            <div class="tipss"></div>
          </div>
        </div>

        <!--<div class="form-group" style="margin-bottom: 0; padding-bottom: 0">-->
          <!--<div class="label">-->
            <!--<label>{:L("xiangqingtupian")}：</label>-->
          <!--</div>-->
          <!--<div class="field">-->
            <!--<div id="zyUpload" class="demo"></div>-->
          <!--</div>-->
        <!--</div>-->

        <!--<div class="form-group">-->
          <!--<div class="label">{:L("dianjishanchu")}</div>-->
          <!--<div class="field" id="myimg">-->
            <!--<style>-->
              <!--#myimg {width: 650px}-->
              <!--#myimg img{border: #ccc 3px solid; margin: 4px; cursor: pointer; height: 120px;}-->
              <!--#myimg img:hover{border: #f00 3px solid;}-->
            <!--</style>-->
            <!--<foreach name="img_list" item="v">-->
              <!--<img src="{$v['goods_imgurl']}" class="img{$v['goods_img_id']}" onclick="return del({$v['goods_img_id']},{$v['goods_id']},this)">-->
            <!--</foreach>-->
          <!--</div>-->
        <!--</div>-->

        <div class="form-group">
          <div class="label">
            <label>{:L("xiaoshoujia")}：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" name="price" value="{$info['price']}" data-validate="required:{:L('qingshuru')}{:L('xiaoshoujia')}"/>
            <div class="tips"></div>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label>{:L("shichangjia")}：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" name="market_price" value="{$info['market_price']}"/>
            <div class="tips"></div>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label>{:L("kucun")}：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" name="stock" value="{$info['stock']}" data-validate="number:{:L('hefakucun')}" />
            <div class="tips"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label>{:L("shangpinshuxing")}：</label>
          </div>
          <div class="field">
            <select name="goods_type" class="input w50">
              <option value="0">{:L("weikaiqi")}</option>
              <option value="1" <if condition="$info['goods_type'] eq 1">selected="selected"</if>>{:L("kaiqi")}</option>
            </select>
            <div class="tipss"></div>
          </div>
        </div>

        <style>
          #mytype input{ float: none; display: inline; }
          .columnList {border-collapse: collapse;}
          .columnList th {background-color: #eee;}
          .columnList th,.columnList td {padding: 5px 10px;border: 1px solid #ddd;text-align: center;vertical-align: middle;}
          .columnList input{padding: 3px;}
          #mytype{background: #f0f0f0; padding: 10px 0;}
          #mytypeitem{padding: 10px 0;}
        </style>
        <div id="mytype" <if condition="$info['goods_type'] neq 1">style="display: none"</if>>
          <if condition="$info['goods_type'] neq 1">
            <div class="form-group">
              <div class="label"><label>{:L("guige")}：</label></div>
              <div class="field">
                <input type="text" class="Father_Title input w50" name="spec_name[]" placeholder="{:L('guigemingchen')}"/>　
                <input type="text" class="input w50 check_item" name="item_name[]" placeholder="{:L('guigeshuxing')}"/>　
                <a href="javascript:void(0)" class="mya1 button bg-main">{:L("tianjiaguige")}</a>　
                <a href="javascript:void(0)" class="button bg-main" onclick="refreshOptions();">{:L("scguigebiao")}</a>
              </div>
            </div>
            <else/>
            {$mytype}
          </if>
        </div>

        <div id="mytypeitem" <if condition="$info['goods_type'] neq 1">style="display: none"</if>>
          <div class="form-group">
            <div class="label"><label>{:L("guigebiao")}：</label></div>
            <div class="field" id="createTable">{$mytypeitem}</div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label>{:L("qitashuxing")}：</label>
          </div>
          <div class="field" style="padding-top:8px;"> 
            {:L("xianshi")} <input name="is_show" type="checkbox" value="1" <if condition="$info['is_show'] eq 1">checked="checked"</if>/>
            {:L("zhuye")} <input name="is_home" type="checkbox" value="1" <if condition="$info['is_home'] eq 1">checked="checked"</if>/>
            {:L("remen")} <input name="is_hot" type="checkbox" value="1" <if condition="$info['is_hot'] eq 1">checked="checked"</if>/>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label>{:L("shangpinjianjie")}：</label>
          </div>
          <div class="field">
            <textarea class="input w50" name="goods_introduce" style="width: 650px">{$info['goods_introduce']}</textarea>
            <div class="tips"></div>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label>{:L("paixu")}：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" name="sort" value="{$info['sort']}" data-validate="number:{:L('hefapaixu')}" />
            <div class="tips"></div>
          </div>
        </div>

        <!--<div class="form-group">-->
          <!--<div class="label">-->
            <!--<label>{:L("xiangqing")}：</label>-->
          <!--</div>-->
          <!--<div class="field" style="padding-top:8px;">-->
            <!--<script id="editor" type="text/plain" style="width:90%;height:400px;" name="goods_describe">{$info['goods_describe']}</script>-->
          <!--</div>-->
        <!--</div>-->
        <div class="form-group">
          <div class="label">
            <label></label>
          </div>
          <div class="field">
            <button class="button bg-main icon-check-square-o" type="submit"> {:L("tijiao")}</button>
          </div>
        </div>

      </form>
    </div>
  </div>
  <script type="text/javascript">
    $(document).ready(function(){
      $("select[name='goods_type']").change(function(){
        var typeon = $("select[name='goods_type']").val();
        if(typeon==1){$("#mytype").show();}
        else{$("#mytype").hide();$("#mytypeitem").hide();}
      });

      $(".mya1").click(function(){
        var html = '<div class="form-group"> ';
        html += '<div class="label"><label></label></div>';
        html += '<div class="field"> ';
        html += '<input type="text" class="Father_Title input w50" name="spec_name[]" placeholder="{:L("guigemingchen")}" data-validate="required: " />　';
        html += ' <input type="text" class="input w50 check_item" name="item_name[]" placeholder="{:L("guigeshuxing")}" data-validate="required: " />　';
        html += ' <a href="javascript:void(0)" class="mya1 button bg-red" onclick="itemdel(this)">{:L("shanchuguige")}</a>';
        html += '</div></div>';

        $("#mytype").append(html);
      });
    });

    function preview(file)
    {
      var prevDiv = document.getElementById('preview');
      if (file.files && file.files[0])
      {
        var reader = new FileReader();
        reader.onload = function(evt){
          prevDiv.innerHTML = '<img src="' + evt.target.result + '" width="100%" />';
        }
        reader.readAsDataURL(file.files[0]);
      }
      else
      {
        prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
      }
    }
    //单个删除
    function del(id,goods_id,t){
      if(confirm("{:L('shanchuxiaoxi')}")){
        var url = "{:U('goods/delpic')}";
        $.getJSON(url,{id:id,goods_id:goods_id},function (data){
          if(data.result=="true1"){$(t).remove();}
          else if(data.result=="true2"){$(t).next('input[type=hidden]').remove();$(t).remove();}
        })
      }
    }
    function itemdel(t){
      $(t).parents('.form-group').remove();
    }
    //生成表格
    function refreshOptions(){
      $("#mytypeitem").show();
      step.Creat_Table();
    }
  </script>
</body>
</html>